package dimensional.traveler.client.screens.missionend;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.TextArea;

import dimensional.traveler.client.mvc.ControlledComposite;
import dimensional.traveler.client.mvc.MediatedController;
import dimensional.traveler.shared.Settings;
import dimensional.traveler.shared.model.Mission;
import dimensional.traveler.shared.model.missionSequence.MissionEndDetails;

public class MissionEndScreen extends ControlledComposite implements SelectHandler {

	private static MissionEndScreenUiBinder uiBinder = GWT.create(MissionEndScreenUiBinder.class);

	interface MissionEndScreenUiBinder extends UiBinder<Widget, MissionEndScreen> {
	}

	@UiField
	ContentPanel backgroundPanel;
	@UiField
	ContentPanel contentPanel;
	@UiField
	TextButton finishButton;

	private MissionEndController controller = new MissionEndController();
	private Mission mission;

	public MissionEndScreen(MissionEndDetails missionEndDetails) {
		initWidget(uiBinder.createAndBindUi(this));
		setupFinishButton();
		setupContent(missionEndDetails);
	}

	private void setupFinishButton() {
		finishButton.addSelectHandler(this);
	}

	@Override
	public MediatedController getController() {
		return controller;
	}

	@Override
	public void onSelect(SelectEvent event) {
		controller.showNextScreen();
	}

	public void setupContent(MissionEndDetails missionEndDetails) {
		setupBackgroundImage(missionEndDetails);
		displayMissionDetails(missionEndDetails);
	}

	private void setupBackgroundImage(MissionEndDetails missionEndDetails) {
		String imagePath;
		if (Settings.USE_PLACEHOLDERS) {
			imagePath = "/images/missions/generic/placeholder.jpg";
		} else {
			if (missionEndDetails.victorious) {
				if (mission.isHavingVictoryImage()) {
					imagePath = "/images/missions/" + mission.getMissionId() + "/victory.jpg";
				} else {
					imagePath = "/images/missions/generic/victory.jpg";
				}
			} else {
				if (mission.isHavingDefeatImage()) {
					imagePath = "/images/missions/" + mission.getMissionId() + "/defeat.jpg";
				} else {
					imagePath = "/images/missions/generic/defeat.jpg";
				}
			}
		}
		backgroundPanel.setBodyStyle("background:white url('" + imagePath + "') no-repeat top right");
	}
	
	private void displayMissionDetails(MissionEndDetails missionEndDetails) {
		contentPanel.setBodyStyle("background-color: transparent");
		// TODO if mission details is really complex, I can use a custom widget
		// instead of a textArea
		TextArea contentText = new TextArea();
		contentText.setValue("Enemies killed: " + missionEndDetails.enemiesKilled);
		contentPanel.add(contentText);
	}

}
